<!-- 部门通知-->
<template>
    <view class="mainPge">
        <!-- 公共头部 -->
        <headerPublic title="部门通知" />
        <!--主体-->
        <view class="page-content">
            <!-- 顶部固定 -->
            <view class="tabs">
                <view class="tbrow">
                    <view class="tabit" v-for="(it, index) in tabs" :key="index" @click="handeltabcheck(it.value)">
                        <text class="t1">{{ it.lable }}</text>
                        <view class="notice-red" v-if="index==0 && noticeReadNum!==0">{{noticeReadNum}}</view>
                        <view class="notice-red" v-if="index==2 && iproveReadNum!==0">{{iproveReadNum}}</view>
                        <view class="activeli" :class="{ check: tabcheck == it.value }"></view>
                    </view>
                </view>
                <!--搜索-->
                <view class="top-search">
                    <uv-input v-model="title" placeholder="搜索" class="search-ipt" border="none">
                        <template v-slot:prefix>
                            <uv-icon name="search" size="24" color="#999" @click="$refs.paging.reload()"></uv-icon>
                        </template>
                    </uv-input>
                </view>
            </view>
            <!-- 列表-->
            <z-paging :fixed="false" style="height: 81vh" ref="paging" v-model="list" @query="querylist" :auto="false">
                <!-- 部门通知-->
                <view class="Plist" v-if="tabcheck == 1">
                    <view class="Pitme" v-for="(item, index) in list" :key="title">
                        <view class="pstatus">
                            <view class="pstatus">
                                <!-- b1红色  b2 黄色  b3 绿色  没有就是默认灰色-->
                                <text class="str" :class="{ b1: item.readStatus == 'UNREAD', b2: item.readStatus == 'READED' }">{{ item.readStatusName }}</text>
                            </view>
                        </view>
                        <text class="ptitle">{{ item.title }}</text>
                        <view class="pt-row">
                            <view class="ptit">
                                <text class="t1">创建部门：</text>
                                <text class="t2">{{ item.agentOrgName }}</text>
                            </view>
                            <view class="ptit">
                                <text class="t1">创建人：</text>
                                <text class="t2">{{ item.agentName }}</text>
                            </view>
                        </view>
                        <view class="pt-row">
                            <view class="ptit">
                                <text class="t1">公告日期：</text>
                                <text class="t2">{{ item.createTime }}</text>
                            </view>
                        </view>
                        <view class="line"></view>
                        <view class="pt-row">
                            <text class="t1">我收到的</text>
                            <view class="pt-row bts">
                                <view class="bt bt-type2" @click="ljkook(item)">立即查看</view>
                            </view>
                        </view>
                    </view>
                </view>
                <!-- 我创建的-->
                <view class="Plist" v-if="tabcheck == 2">
                    <view class="Pitme" v-for="(item, index) in list" :key="title">
                        <view class="pstatus">
                            <!-- b1红色  b2 黄色  b3 绿色  没有就是默认灰色-->
                            <text 
                                class="str"
                                :class="{
                                    'b2': ['UNDO', 'AUDIT'].includes(item.businessStatus),
                                    'b3': ['DONE', 'AUDITED', 'FINISH'].includes(item.businessStatus)
                                }"
                            >
                                {{item.businessStatusName}}
                            </text>
                        </view>
                        <text class="ptitle">{{ item.title }}</text>
                        <view class="pt-row">
                            <view class="ptit">
                                <text class="t1">创建部门：</text>
                                <text class="t2">{{ item.deptName }}</text>
                            </view>
                            <view class="ptit">
                                <text class="t1">创建人：</text>
                                <text class="t2">{{ item.agentName }}</text>
                            </view>
                        </view>
                        <view class="pt-row">
                            <view class="ptit">
                                <text class="t1">公告日期：</text>
                                <text class="t2">{{ item.publishTime }}</text>
                            </view>
                        </view>
                        <view class="line"></view>
                        <view class="pt-row">
                            <text class="t1">我创建的</text>
                            <view class="pt-row bts">
                                <view
                                    class="bt bt-type1"
                                    v-if="item.businessStatus == 'UNDO' || item.businessStatus == 'AUDIT'"
                                    @click="
                                        $refs.modal.open();
                                        businessId = item.businessId;
                                    "
                                >
                                    撤回
                                </view>
                                <view
                                    class="bt bt-type1"
                                    @click="goinfopage(item)"
                                    v-if="item.businessStatus == 'UNDO' || item.businessStatus == 'FINISH' || item.businessStatus == 'DONE'|| item.businessStatus == 'AUDIT'"
                                >
                                    查看详情
                                </view>
                                <view
                                    class="bt bt-type2"
                                    v-if="item.businessStatus == 'AUDITED' || item.businessStatus == 'DRAFT' || item.businessStatus == 'REBACK'"
                                    @click="ljbnfn(item)"
                                >
                                    立即办理
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
                <!-- 我审批的-->
                <view class="Plist" v-if="tabcheck == 3">
                    <view class="Pitme" v-for="(item, index) in list" :key="title">
                        <view class="pstatus">
                            <view class="pstatus">
                                <!-- b1红色  b2 黄色  b3 绿色  没有就是默认灰色-->
                                <text class="str b1" :class="{ b1: item.status == 'DRAFT', b2: item.status == 'UNDO', b3: item.status == 'DONE' }">{{ item.statusName }}</text>
                            </view>
                        </view>
                        <text class="ptitle">{{ item.title }}</text>
                        <view class="pt-row">
                            <view class="ptit">
                                <text class="t1">创建部门：</text>
                                <text class="t2">{{ item.agentOrgName }}</text>
                            </view>
                            <view class="ptit">
                                <text class="t1">创建人：</text>
                                <text class="t2">{{ item.agentName }}</text>
                            </view>
                        </view>
                        <view class="pt-row">
                            <view class="ptit">
                                <text class="t1">创建日期：</text>
                                <text class="t2">{{ item.createTime }}</text>
                            </view>
                        </view>
                        <view class="line"></view>
                        <view class="pt-row">
                            <text class="t1">我处理的</text>
                            <view class="pt-row bts">
                                <view class="bt bt-type1" @click="goinfopage(item)" v-if="item.status == 'DONE' || item.status == 'REBACK'">查看详情</view>
                                <view class="bt bt-type2" v-if="item.status == 'DRAFT' || item.status == 'UNDO'" @click="ljbnfn(item)">立即办理</view>
                            </view>
                        </view>
                    </view>
                </view>
            </z-paging>
            <!-- 新建按钮 -->
            <view class="newadd" @click="addnewfn">新建</view>
        </view>
        <!-- 二次确认提示消息框-->
        <uv-modal ref="modal" title="提示" content="您是否确认撤回?" @confirm="handelreback" :showCancelButton="true" cancelColor="#2D75D7" align="center"></uv-modal>
    </view>
</template>

<script>
import { getICreatList, getIapproveList, getNoticeList, creatReback, setReadstauts } from '@/api/partNotice.js';
import {getreadNum} from '@/api/common.js';
export default {
    name: 'header',
    data() {
        return {
            list: [],
            title: '',
            tabcheck: 2,
            pageSize: 10,
            pageNumber: 1,
            tabs: [
                {
                    lable: '部门通知',
                    value: 1
                },
                {
                    lable: '我创建的',
                    value: 2
                },
                {
                    lable: '我审批的',
                    value: 3
                }
            ],
            businessId: '',
			noticeReadNum: 0, // 未读通知数量
			iproveReadNum: 0, // 未处理审批数量
			timer: null // 定时器
        };
    },
    onLoad(res) {
        this.tabcheck = res && res.tabtype ? parseInt(res.tabtype) : 1;
		// 首次加载时调用一次
		this.updateReadNum();
    },
    onShow() {
    	this.list=[]
    	this.pageNumber=1
    	this.getList()
		this.startReadNumTimer()
    },
	onHide() {
		this.clearReadNumTimer()
	},
	onUnload() {
		this.clearReadNumTimer()
	},
    methods: {
		// 更新未读数量
		updateReadNum() {
			getreadNum().then(res => {
				if (res.code == 200 && res.data) {
					this.noticeReadNum = res.data["UN_READ_COUNT_DEPART"]?.count || 0;
					this.iproveReadNum = res.data["UN_HANDLE_COUNT_DEPART"]?.count || 0;
				}
			}).catch(error => {
				console.error('获取未读数量失败:', error);
				// 请求失败时停止定时器，避免继续请求
				this.clearReadNumTimer();
			});
		},
		startReadNumTimer() {
			// 清除可能存在的定时器
			this.clearReadNumTimer()
			// 设置新的定时器
			this.timer = setInterval(() => {
				this.updateReadNum()
			}, 3000)
		},
		// 清除定时器方法
		clearReadNumTimer() {
			if (this.timer) {
				clearInterval(this.timer)
				this.timer = null
			}
		}, 
        // 我创建的撤回
        handelreback() {
            creatReback({ id: this.businessId }).then((res) => {
                this.$refs.paging.reload();
            });
        },
        // 部门通知---立即查看
        ljkook(it) {
            //部门通知---立即查看
            if (this.tabcheck == 1) {
                setReadstauts({ noticeId: it.id });
                uni.navigateTo({
                    url: '/documentManagement/departmentNotice/conduct?tabtype=notice&businessId=' + it.businessId + '&businessType=' + it.businessType + '&id=' + it.id
                });
            }
        },
        // 立即办理
        ljbnfn(it) {
            //我创建的---立即办理
            if (this.tabcheck == 2) {
                // 已审批 跳16
                if (it.businessStatus == 'DRAFT' || it.businessStatus == 'REBACK') {
                    uni.navigateTo({
                        url:
                            '/documentManagement/departmentNotice/infoset?tabtype=icreat&actionType=edit&businessId=' +
                            it.businessId +
                            '&businessType=' +
                            it.businessType +
                            '&businessStatus=' +
                            it.businessStatus
                    });
                }
                if (it.businessStatus == 'AUDITED') {
                    uni.navigateTo({
                        url: '/documentManagement/departmentNotice/conduct?tabtype=icreat&businessId=' + it.businessId + '&businessType=' + it.businessType
                    });
                }
            }
            // 我审批的--立即办理
            if (this.tabcheck == 3) {
                uni.navigateTo({
                    url: '/documentManagement/departmentNotice/conduct?tabtype=iapprove&businessId=' + it.businessId + '&businessType=' + it.businessType + '&id=' + it.id
                });
            }
        },
        // 查看详情
        goinfopage(it) {
            //我创建的---查看详情
            if (this.tabcheck == 2) {
                uni.navigateTo({
                    url: '/documentManagement/departmentNotice/conduct?tabtype=icreat&businessId=' + it.businessId + '&businessType=' + it.businessType
                });
            }
            // 我审批的--查看详情
            if (this.tabcheck == 3) {
                uni.navigateTo({
                    url:
                        '/documentManagement/departmentNotice/conduct?tabtype=iapprove&businessId=' +
                        it.businessId +
                        '&businessType=' +
                        it.businessType +
                        '&id=' +
                        it.id +
                        '&actiontype=seeinfo'
                });
            }
        },
        // tab切换
        handeltabcheck(value) {
            this.tabcheck = value;
			this.list =[]
            this.$refs.paging.reload();
        },
        // 获取页面列表数据
        getList(pageNum) {
            let querydata = {
                pageNumber: this.pageNumber,
                pageSize: this.pageSize,
                title: this.title
            };
            if (this.tabcheck == 1) {
                getNoticeList(querydata).then((res) => {
                    this.setPagelist(res);
                });
            }
            if (this.tabcheck == 2) {
                getICreatList(querydata).then((res) => {
                    this.setPagelist(res);
                });
            }
            if (this.tabcheck == 3) {
                getIapproveList(querydata).then((res) => {
                    this.setPagelist(res);
                });
            }
        },
        setPagelist(res) {
            if (res.data !== [] && res.code == 200) {
                this.$refs.paging.complete(res.data.records);
            }
        },
        // 立即办理
        goljbl() {
            uni.navigateTo({
                url: '/documentManagement/departmentNotice/conduct'
            });
        },
        // 新建
        addnewfn() {
            uni.navigateTo({
                url: '/documentManagement/departmentNotice/infoset'
            });
        },
        querylist(page, size) {
            this.pageSize = size;
            this.pageNumber = page;
            this.getList();
        },
        // 返回上一层
        backfn() {
            uni.navigateBack();
        }
    }
};
</script>

<style scoped lang="scss">
@import '../../style/documentmanage.scss';
</style>
